<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-waiting-current-waiting" :style="getStyle('area')">
      <!--      {{currentPatientInfo}}-->
      <!--      {{waitPatientInfo}}-->
      <div class="patient-area">
        <div class="current-area">
          <div class="title" :style="getStyle('currentTitle')">
            <ScrollX>{{ optionsInfo['currentPaperwork'] }}</ScrollX>
          </div>
          <div class="content">
            <p v-for="(itemInfo,index) in currentPatientInfo" :key="`${itemInfo.orderNo}-${index}`"
               :style="getStyle('currentTitle')">
              {{ itemInfo.orderNo || '\u00a0' }}
              {{ itemInfo.patientName || '\u00a0' }}
            </p>
          </div>
        </div>
        <div class="waiting-area">
          <div class="title" :style="getStyle('waitingTitle')">
            <ScrollX>{{ optionsInfo['waitingPaperwork'] }}</ScrollX>
          </div>
          <div class="content">
            <div class="content-inside"
                 v-for="(itemInfo, index) in waitPatientInfo"
                 :key="index"
                 :data-key="index"
                 :class="{'even':((index+1)%2)===0}"
            >
              <div
                  class="content-inside-child"
                  v-for="(itemInfo2, itemInfoKey2) in itemInfo"
                  :key="`${itemInfo2.queuingId}-${itemInfoKey2}`"
                  :style="getStyle('waitingTitle')"
              >
                <div class="child-inside" v-if="true">
                  {{ itemInfo2.orderNo || '\u00a0' }}{{ itemInfo2.patientName || '\u00a0' }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {pxToVw} from '@/utils/common';
import emitter from "@/utils/designer/emitter";
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import {buildParentApi} from "@/utils/designer/util";

let buildParent = buildParentApi();
export default {
  name: "scene-waiting-current-waiting-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    CompOperate,
    ScrollX
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  data() {
    return {
      optionsInfo: Object()
    }
  },

  computed: {
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'area':
            Reflect.set(Obj, 'height', pxToVw(info.areaHeight));
            return Obj;
          case "currentTitle":
            Reflect.set(Obj, 'fontSize', pxToVw((info.currentFontSize) || 50));
            return Obj;
          case "waitingTitle":
            Reflect.set(Obj, 'fontSize', pxToVw((info.waitingFontSize) || 50));
            return Obj;
        }
      }
    },

    //  队列-患者-数据
    doctorPatientList() {
      let data;
      let devType = (this.devType);
      let WaitingPatientList = this.$store.getters['getWaitingPatientInfo'];
      if (devType) {
        data = (WaitingPatientList.apiData)
      } else {
        data = (WaitingPatientList.mockData)
      }
      if (data && !!(data.length)) {
        return data.slice(0, 1);
      } else {
        return [{waitPatientList: []}]
      }
    },


    //  队列-患者-数据
    doctorPatientListInfo() {
      let data;
      let devType = (this.devType);
      let WaitingPatientList = this.$store.getters['getWaitingPatientInfo'];
      if (devType) {
        data = (WaitingPatientList.apiData)
      } else {
        data = (WaitingPatientList.mockData)
      }
      return data || [{waitPatientList: []}]
    },

    currentPatientInfo() {
      let list = (this.doctorPatientListInfo);
      let summary = this.$lodash.map(list, "treatingPatient");
      let flattenArr = this.$lodash.flattenDeep(summary);
      let uniqByArr = this.$lodash.uniqBy(flattenArr, 'queuingId');
      return uniqByArr.slice(0, 1);
    },


    waitPatientInfo() {
      let list = (this.doctorPatientListInfo);
      let summary = this.$lodash.map(list, "waitPatientList");
      let flattenArr = this.$lodash.flattenDeep(summary);
      let uniqByArr = this.$lodash.uniqBy(flattenArr, 'queuingId');
      if (uniqByArr.length > 16) {
        let flattenArr2 = uniqByArr.slice(0, 16);
        return this.$lodash.chunk(flattenArr2, 4)
      } else {
        let arr = (new Array(16 - uniqByArr.length).fill({}));
        let flattenArr2 = [...uniqByArr, ...arr];
        return this.$lodash.chunk(flattenArr2, 4)
      }
    },

    // 当前就诊患者
    treatingPatientInfo() {
      return function (info) {
        if (!!(info.treatingPatient) && !!(info.treatingPatient.length)) {
          return (info.treatingPatient).slice(0, 1)
        } else {
          return []
        }
      }
    },

    // 切割候诊患者
    waitingChunkInfos() {
      return function (info) {
        if (info.length > 16) {
          let flattenArr2 = info.slice(0, 16);
          return this.$lodash.chunk(flattenArr2, 4)
        } else {
          let arr = (new Array(16 - info.length).fill({}));
          let flattenArr2 = [...info, ...arr];
          return this.$lodash.chunk(flattenArr2, 4)
        }
      }
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "patientListInfo":
          let patientList = this.$lodash.cloneDeep(data);
          buildParent.executeParentMethod('writeLog', JSON.stringify({sign: '模板-患者信息', key: key, data: data}));
          this.$store.dispatch('setWaitingPatientInfo', patientList)
          break
      }
    }
  },

  watch: {
    field: {
      handler: function (val) {
        let key = (val.options.config);
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },
};
</script>

<style lang="scss" scoped>
.scene-waiting-current-waiting {
  padding: 28px 30px 10px 30px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  color: #333;
  height: auto;
  font-size: 50px;
  background: #E5EAEF;

  .dept-area {
    font-weight: 500;
    font-size: 110px;
    height: 220px;
    color: #FFFFFF;
    line-height: 1.5;
    width: 100%;
    text-align: center;
    background: #0059B3;
    padding: 30px 0;
    overflow: hidden;
    border-radius: 32px 32px 0 0;
    @include singleEllipsis(100%)
  }

  .patient-area {
    overflow: inherit;
    border-radius: 32px 32px;
    overflow: hidden;
    background: transparent;
    box-shadow: 30px 30px 30px -10px rgba(0, 0, 0, 0.15),
    inset 5px 5px 10px rgba(255, 255, 255, 0.75),
    inset -1px -1px 10px rgba(0, 0, 0, 0.2);

    .current-area {
      display: flex;
      height: 140px;
      flex-direction: row;
      overflow: hidden;
      border-radius: 0 0 24px 24px;
      background: rgba(0, 89, 179, 1);

      .title {
        color: white;
        font-size: 40px;
        height: 100%;
        box-sizing: border-box;
        width: 260px;
        background: rgba(0, 71, 143, 1);
        display: flex;
        padding: 0 20px;
        justify-content: center;
        align-items: center;
        align-self: center;

        .scrollText {
          width: 100%;
        }
      }

      .content {
        color: white;
        padding: 42px 0;
        display: flex;
        flex-direction: row;
        flex: 1;
        height: 100%;
        align-self: center;
        justify-content: center;
        align-items: center;

        p {
          font-size: 54px;
          flex: 1;
          color: white;
          text-align: center;
        }
      }
    }

    .waiting-area {
      display: flex;
      margin-top: 14px;
      flex-direction: column;
      border-radius: 24px 24px 0 0;
      overflow: hidden;

      .title {
        background: linear-gradient(rgba(244, 244, 244, 1) 0%, rgba(240, 240, 240, 1) 100%);
        text-align: center;
        font-size: 50px;
        height: 120px;
        width: 100%;
        color: #0059B3;
        border-bottom: 2px solid #DDDDDD;
        @include fontWeight();
        display: flex;
        padding: 0 20px;
        align-self: center;
        align-items: center;
        justify-content: center;

        .scrollText {
          width: 100%;
          height: 100%;
          display: flex;
          justify-content: center;
          align-items: center;
          align-self: center;
        }
      }

      .content {
        background: white;
        display: flex;
        flex-wrap: wrap;

        .content-inside {
          width: 100%;
          display: flex;
          padding: 8px 0;
          flex-wrap: wrap;

          .content-inside-child {

            font-weight: 400;
            text-align: center;
            display: flex;
            flex-basis: calc(100% / 4);
            flex-shrink: 0;
            justify-content: center;
            align-self: center;
            align-items: center;
            height: 108px;
            font-size: 54px;
            color: #333333;
            overflow: hidden;

            .child-inside {
              padding: 0 24px;
              @include singleEllipsis(100%)
            }
          }
        }
      }

      .even {
        background: rgba(244, 244, 244, 1);
      }
    }
  }


}
</style>
